package com.kanshu.ksgb.fastread.doudou.module.download.utils;

import android.text.TextUtils;

import com.kanshu.ksgb.fastread.doudou.module.download.bean.DownLoadChapterBean;

import java.util.ArrayList;
import java.util.List;

/**
 * http://blog.csdn.net/lmj623565791/article/details/40212367
 *
 * @author zhy
 */
public class TreeHelper {

    /**
     * 过滤出所有可见的DownLoadChapterBean
     *
     * @param DownLoadChapterBeans
     * @return
     */
    public static List<DownLoadChapterBean> filterVisibleNode(List<DownLoadChapterBean> DownLoadChapterBeans) {
        List<DownLoadChapterBean> result = new ArrayList<DownLoadChapterBean>();

        for (DownLoadChapterBean bean : DownLoadChapterBeans) {
            // 如果为跟节点
            if (!TextUtils.isEmpty(bean.id)) {
                result.add(bean);
            }

            // 如果上层目录为展开状态
            if(bean.is_show){
                if (bean.is_download || !bean.is_canceldownload) {
                    for (DownLoadChapterBean childBean : bean.chapters) {
                        childBean.is_download = bean.is_download;
                        result.add(childBean);
                    }
                } else {
                    result.addAll(bean.chapters);
                }
            }
        }
        return result;
    }

    public static List<DownLoadChapterBean> getRootNodes(List<DownLoadChapterBean> DownLoadChapterBeans) {
        List<DownLoadChapterBean> root = new ArrayList<>();
        for (DownLoadChapterBean bean : DownLoadChapterBeans) {
            if (!TextUtils.isEmpty(bean.id))
                root.add(bean);
        }
        return root;
    }

    /**
     * 把一个节点上的所有的内容都挂上去
     */
    private static void addNode(List<DownLoadChapterBean> DownLoadChapterBeans, DownLoadChapterBean DownLoadChapterBean,
                                int defaultExpandLeval, int currentLevel) {

//        DownLoadChapterBeans.add(DownLoadChapterBean);
//        if (defaultExpandLeval >= currentLevel) {
//            DownLoadChapterBean.setExpand(true);
//        }
//
//        if (DownLoadChapterBean.isLeaf())
//            return;
//        for (int i = 0; i < DownLoadChapterBean.getChildren().size(); i++) {
//            addDownLoadChapterBean(DownLoadChapterBeans, DownLoadChapterBean.getChildren().get(i), defaultExpandLeval,
//                    currentLevel + 1);
//        }
    }
}
